package com.chenglu.android.search.mvp.presenter;




import com.chenglu.android.common.base.BasePresenter;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.net.RxScheduler;
import com.chenglu.android.common.net.exception.NetErrorException;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.index.bean.IndexRecomBean;
import com.chenglu.android.search.bean.SearchBean;
import com.chenglu.android.search.mvp.contract.SearchContract;
import com.chenglu.android.search.mvp.model.SearchModel;

import java.util.Map;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;


/**
 * @author: lyj
 * @Created by Copyright 2023/3/2 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description presenter
 */
public class SearchPresenter extends BasePresenter<SearchContract.View> implements SearchContract.SearchPresenter {

    private  SearchContract.SearchModel model;

    public SearchPresenter(){
        model = new SearchModel();
    }


    @Override
    public void getSearchPage(Map<String, String> map) {

        if(!isViewAttached()){
            return;
        }
        model.getSearchPage(map)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<IndexRecomBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull IndexRecomBean searchBean) {
                        switch (searchBean.getCode()){
                            case Constant.HTTPCODE200 :
                                mView.getSearchPage(searchBean);
                                break;
                            case Constant.HTTPCODE400:
                            case Constant.HTTPCODE401:
                            case Constant.HTTPCODE404:
                            case Constant.HTTPCODE500:
                                ToastUtils.show(searchBean.getMsg());
                                LogUtils.v("ruby---",searchBean.getMsg());
                                break;
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        LogUtils.v("ruby---",e.getMessage());
                        NetErrorException.apiError(e,mView);
                    }

                    @Override
                    public void onComplete() {

                    }
                });


    }
}
